package rabbitmq.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import rabbitmq.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Productor {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1. 创建对应的工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL_HOST);
        // 2. 创建connection
        Connection connection = connectionFactory.newConnection();
        // 3. 创建 channel
        Channel channel = connection.createChannel();
        // 4. 设置queue 和 exchange
        channel.queueDeclare(Constants.WORK_QUEUE, true, false, true, null);
        for (int i = 0; i < 10; i++) {
            String msg = "hello worker pattern..." + i;
            channel.basicPublish("", Constants.WORK_QUEUE, null, msg.getBytes());
        }

        System.out.println("传输完成");
        channel.close();
        connection.close();

    }
}
